package com.xiaozhen.common.core.utils;


import com.xiaoleilu.hutool.convert.Convert;
import com.xiaozhen.common.core.constant.Constants;

import java.lang.reflect.Type;

/**
 * @author zhenweisheng
 * @date 2020/8/14 14:54
 * @description:
 */
public class ConvertUtils {

    /**
     * 通过类型转换数据
     *
     * @param type  String Type, short/int/long/float/double/boolean/string
     * @param value String Value
     * @param <T>   T
     * @return T
     */
    public static <T> T value(String type, String value) {
        return Convert.convert(getTypeClassName(type), value);
    }


    /**
     * 获取基本类型 Class Name
     *
     * @param type String Type, short/int/long/float/double/boolean/string
     * @return Class Type
     */
    public static Type getTypeClassName(String type) {
        Type classType = String.class;
        switch (type.toLowerCase()) {
            case Constants.ValueType.BYTE:
                classType = Byte.TYPE;
                break;
            case Constants.ValueType.SHORT:
                classType = Short.TYPE;
                break;
            case Constants.ValueType.INT:
                classType = Integer.TYPE;
                break;
            case Constants.ValueType.LONG:
                classType = Long.TYPE;
                break;
            case Constants.ValueType.FLOAT:
                classType = Float.TYPE;
                break;
            case Constants.ValueType.DOUBLE:
                classType = Double.TYPE;
                break;
            case Constants.ValueType.BOOLEAN:
                classType = Boolean.TYPE;
                break;
            case Constants.ValueType.ENUM:
                classType = Enum.class;
                break;
            default:
                break;
        }
        return classType;
    }


    public static Object bytes2Value(String type, byte[] value) {
        Object result = null;
        switch (type.toLowerCase()) {
            case Constants.DataType.BYTES:
                result = value;
                break;
            case Constants.DataType.BYTE:
                result = value[0];
                break;
            case Constants.DataType.SHORT:
                result = ByteArrayUtil.bytes2Short(value);
                break;
            case Constants.DataType.INT:
                result = ByteArrayUtil.bytes2int(value);
                break;
            case Constants.DataType.LONG:
                result = Convert.bytesToLong(value);
                break;
            case Constants.DataType.BIT:
                result = ByteArrayUtil.bytes2bitStr(value);
                break;
            case Constants.DataType.HEX:
                result = ByteArrayUtil.bytes2HexStr(value);
                break;
            case Constants.DataType.FULL_HEX:
                result = ByteArrayUtil.bytes2FullHexStr(value);
                break;
            case Constants.DataType.GBK:
                result = ByteArrayUtil.bytes2gbkString(value);
                break;
            case Constants.DataType.LON_OR_LAT:
                result = ByteArrayUtil.parseLonOrLat(value);
                break;
            default:
                break;
        }
        return result;
    }
}
